package com.heima.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.user.dto.FollowBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author: itheima
 * @create: 2022-03-29 15:31
 */
@Component
@Slf4j
public class FollowBehaviorListener {

    @Autowired
    private IApFollowBehaviorService followBehaviorService;

    /**
     * 关注行为监听器
     *
     * @param record
     */
    @KafkaListener(topics = "${topic.followBehaviorTopic}")
    public void handleMessage(ConsumerRecord<String, String> record) {
        try {
            if (StringUtils.isNotBlank(record.value())) {
                log.info("行为微服务，关注行为监听器：{}", record.value());
                FollowBehaviorDto followBehaviorDto = JSON.parseObject(record.value(), FollowBehaviorDto.class);
                followBehaviorService.saveFollowBehavior(followBehaviorDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("行为微服务，关注行为保存异常：{}", e);
        }

    }
}
